package com.qiandw.enterprise.credit.common

import javax.servlet.http.HttpServletRequest

import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.{ControllerAdvice, ExceptionHandler, ResponseBody}
import com.qiandw.enterprise.credit._

/**
  * Created by Draven on 2018/7/10.
  * Email:hjx@qiandw.com
  */
@ControllerAdvice(Array("com.qiandw.enterphrise.credit.controller"))
@SuppressWarnings(Array("unchecked"))
class SysExceptionHandleController {

  val logger = LoggerFactory.getLogger(classOf[SysExceptionHandleController])

  @ResponseBody
  @ExceptionHandler(value = Array(classOf[FormValidationException]))
  def argumentExceptionHandler(req: HttpServletRequest, e: Exception): SysResp = HandleExcption(SYS_MSG_ILLEGAL_ARGUMENT, e, req.getRequestURI)

  @ResponseBody
  @ExceptionHandler(value = Array(classOf[Exception]))
  def defaultExceptionHandler(req: HttpServletRequest, e: Exception): SysResp = HandleExcption(SYS_MSG_ERROR, e, req.getRequestURI)

  def HandleExcption(msg: (Int,String, String), e: Exception, uri: String): SysResp = {
    logger.error(uri, e)
    val resp = new SysResp
    resp.code = msg._1
    resp.msg = msg._2
    resp.errDetail = msg._3
    resp
  }
}
